package com.wq.wrongquestion.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wq.wrongquestion.entity.bio.BiologyEntity;
import com.wq.wrongquestion.service.bio.BiologyService;
import com.wq.wrongquestion.utils.FileUploadUtils;

@Controller
@RequestMapping("/bio")
public class BiologyController {
    /************************************************Biology Start************************************************/

    final String IMG_PAHT_YT_BIO = "images/bio/yt/";
    final String IMG_PAHT_DA_BIO = "images/bio/da/";

    @Autowired
    public BiologyService biologyService;
    @Autowired
    public FileUploadUtils fileUploadUtils;
    
    @RequestMapping("/biologyInit") 
    public String biologyInit(Model model){
        //章节
        List<String> allZj=biologyService.getBiologyZj();
        
        model.addAttribute("allZj", allZj);
        model.addAttribute("condEntity", new BiologyEntity());
        
        return "bio/biologySearch";
    }


    @PostMapping("/biologyIns")
    public String insBiology(BiologyEntity biologyEntity, @RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile, Model model) {
        // 获取文件名
        String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
        String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();

        // 获取文件路径
        String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_BIO);
        String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_BIO);

        // 保存文件
        try {
            if (ytFile.getSize() > 0) {
                fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                biologyEntity.setImgytnm(fileName);
                biologyEntity.setImgytpath(filePath);
            }
            if (daFile.getSize() > 0) {
                fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                biologyEntity.setImgdanm(fileName2);
                biologyEntity.setImgdapath(filePath2);
            }
            // 插入数据
            biologyEntity.setFxcs(1);
            biologyEntity.setXueke(biologyEntity.getHidXueke());

            biologyService.insBiology(biologyEntity);

            // return "redirect:/bio/biologyInit";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "发生错误,请重试!");
            return "redirect:/bio/addBiology";
        }
        return "redirect:/bio/biologyInit";
    }
    @RequestMapping("/addBiology")
    public String addBiology(Model model){
        List<String> allZj=biologyService.getBiologyZj();
        
        model.addAttribute("allZj", allZj);
        return "bio/addBiology";
    }

    @RequestMapping("/searchBiology")
    public String searchBiology(Model model, @RequestParam(value = "pageNm",defaultValue= "1") Integer pageNum,
            @RequestParam(value="pageSize" ,defaultValue= "10") Integer pageSize, BiologyEntity biologyEntity) {

        PageInfo<BiologyEntity> biology = biologyService.getBiology(pageNum, pageSize, biologyEntity);
        List<String> allZj=biologyService.getBiologyZj();

        model.addAttribute("allZj", allZj);
        model.addAttribute("biology", biology);
        model.addAttribute("condEntity", biologyEntity);

        return "bio/biologySearch";
    }

    @RequestMapping("/searchBiologyByID")
    public String searchBiologyByID(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {

        List<BiologyEntity> biologyByID = biologyService.getBiologyByID(id);
        List<String> allZj=biologyService.getBiologyZj();
        if(!biologyByID.isEmpty()){
            model.addAttribute("biology", biologyByID);
        }else{
            model.addAttribute("msg", "这条记录在数据库中不存在！");
            return "bio/biologySearch";
        }
        
        model.addAttribute("allZj", allZj);

        return "bio/modifyBiology";
    }

    @PostMapping("/biologyUpd")
    public String biologyUpd(Model model, BiologyEntity biologyEntity,@RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile) {
        try {
            // 图片比较
            if(ytFile != null && ytFile.getOriginalFilename() !=null){
                if(!ytFile.getOriginalFilename().startsWith("yt", 0)){
                    // 获取文件名
                    String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_BIO);
                    if (ytFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                        biologyEntity.setImgytnm(fileName);
                        biologyEntity.setImgytpath(filePath);
                    }
                }
            }
            // 图片比较
            if(daFile != null && daFile.getOriginalFilename() !=null){
                if(!daFile.getOriginalFilename().startsWith("da", 0)){
                    // 获取文件名
                    String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_BIO);
                    if (daFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                        biologyEntity.setImgdanm(fileName2);
                        biologyEntity.setImgdapath(filePath2);
                    }                
                }
            }
            // 插入数据
            biologyEntity.setFxcs(1);
            biologyEntity.setXueke(biologyEntity.getHidXueke());

            Integer updRes=biologyService.updBiology(biologyEntity);

            if(updRes==1){
                model.addAttribute("msg", "修改成功！");
            }else{
                model.addAttribute("msg", "修改失败！");
            } 
            List<BiologyEntity> biologyByID = biologyService.getBiologyByID(biologyEntity.getId());           
            if(biologyByID.isEmpty()){
               biologyByID = new ArrayList<BiologyEntity>();
            }
            model.addAttribute("biology", biologyByID);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "文件上传发生错误,请重试!");
            
        }


        return "bio/modifyBiology";   
    }

    @PostMapping("/biologyDel")
    public String biologyDel(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {
        biologyService.delBiology(id);
        
        return "redirect:/bio/biologyInit";
    }    
    /************************************************Biology End************************************************/
}
